<?php
class bops extends spController {

	function crtkz() {
		import("FileUtils.php");
		$dir = 'kz';
		$gender = '女';
		$now = date("Y-m-d H:i:s", time());
		//文件目录名
		$files = scandir($dir);
		$count = count($files);
		$i = 2;

		//echo "xxx";
		//默认 . ..也算一个count
		//echo header("Content-type:text/html;charset=utf-8");
		while ($i < $count) {
			if (!strcmp(strrchr($files[$i], "."), ".jpg")) {
				$file_name = $dir . "/" . $files[$i];
				//echo $file_name;
				$this -> resize($file_name);
				$file_resize_name = substr($file_name, 0, strpos($file_name, ".jpg")) . '_1.jpg';
				$tmp_file = file_get_contents($file_resize_name);
				echo "oo:" . substr($image, 0, strpos($image, ".jpg")) . '_1.jpg' . "<br>";
				$server_dir = "/snapshot/upload/kz" . "/";
				$fileno = FileUtils::getSN();
				$url = $server_dir . $fileno . ".jpg";
				$output_file_name = "upload/kz/" . $fileno . ".jpg";
				$output_file = file_put_contents($output_file_name, $tmp_file);
				//
				$new_photo = array('userid' => 0, 'url' => $url, 'createdate' => $now, 'mdfdate' => $now, 'no' => $fileno, 'gender' => $gender);
				$photo = spDB('ss_photo');
				$photoid = $photo -> create($new_photo);
				echo $output_file_name . "<br>";
				echo $file_name . "成功更名为" . $output_file_name . "<br>";
				unlink($file_resize_name);
				echo "=====";
			}
			$i++;
		}

	}

	function resize($image) {

		$max_width = 220;

		$max_height = 220;

		$size = getimagesize('' . $image);
		//得到图像的大小
		$width = $size[0];
		$height = $size[1];

		$x_ratio = $max_width / $width;
		$y_ratio = $max_height / $height;

		if (($width <= $max_width) && ($height <= $max_height)) {
			$tn_width = $width;
			$tn_height = $height;
		} elseif (($x_ratio * $height) < $max_height) {
			$tn_height = ceil($x_ratio * $height);
			$tn_width = $max_width;
		} else {
			$tn_width = ceil($y_ratio * $width);
			$tn_height = $max_height;
		}

		$src = imagecreatefromjpeg('' . $image);
		$dst = imagecreatetruecolor($tn_width, $tn_height);
		//新建一个真彩色图像
		imagecopyresampled($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height, $width, $height);
		//file_put_contents('kz/xx2.jpg', imagejpeg($dst, null, 100));
		//重采样拷贝部分图像并调整大小
		//header('Content-Type: image/jpeg');

		imagejpeg($dst, substr($image, 0, strpos($image, ".jpg")) . '_1.jpg', 100);
		unlink($image);
		imagedestroy($src);
		imagedestroy($dst);
		return;

	}

	function debug() {
		// $args = array('openid' => '000', 'openkey' => 'B811C64D348DA42250B37BE081EAF328', 'pf' => 'qzone');
		// $url = spUrl('main', 'loginapp', $args);
		// echo "<a href='{$url}'>{$url}</a>";
		$this -> vars = "http://" . IMG_SERVER . "/snapshot/snapshot.swf?userid=0&nickname=test&figureurl=http://thirdapp0.qlogo.cn/qzopenapp/d8219673598dbd6f7bab6efcc1705ca216b06f8c6f8dea7a9875285503236f39/30&gender=男&country=中国&province=广东&city=深圳&appserver=" . APP_SERVER . "&imgserver=" . IMG_SERVER;
		$this -> display("main.html");
	}

	public function photolist() {

		$query = spDB('ss_photo');
		// 这里使用了spPager，同时用spArgs接受到传入的page参数
		$conditons="userid<>0 and status=1";
		$this -> results = $query -> spPager($this -> spArgs('page', 1), 10) -> findAll($conditons, 'createdate desc', null, null);
		// 这里获取分页数据并发送到smarty模板内
		$this -> pager = $query -> spPager() -> getPager();
		$this -> img_server = IMG_SERVER;
		$this -> display("page.html");
	}

	public function lahei() {
		$photoid = $this -> spArgs('photoid');
		$condition = array('id' => $photoid);
		$row = array('status' => '0');
		if ( spDB('ss_photo') -> update($condition, $row)) {echo "ok";
		} else {
			echo "fail";
		};
	}

}
?>